clc
clear
f = [262 330 392];%C调声音频率
Fs = 8000;%采样频率
N = 300;%
Amp = [4 4 4];%令振幅为1
T = 0:1/Fs:3-1/Fs;%声音持续三秒
ph = [0 0 0];%声音相位
sound_c = Amp(1)*sin(2*pi*f(1)*T + ph(1));%声音信号
sound(sound_c,Fs);%以采样频率播放声音

figure('name','第一题')

subplot(3,1,1)
stem(T(1:N),sound_c(1:N))%前N个采样
title('前300个采样信号—时间图')
text(0,-5,'采样频率8k 相位为0')
xlabel('t(s)')
ylabel('A');

subplot(3,1,2)
ph(1) = pi/3;
sound_c = Amp(1)*sin(2*pi*f(1)*T + ph(1));%声音信号
stem(T(1:N),sound_c(1:N))%前N个采样
title('前300个采样信号—时间图')
text(0,-5,'采样频率8k 相位为pi/3')
xlabel('t(s)')
ylabel('A');

subplot(3,1,3)
ph(1) = 0;
Fs = 1000;%1000Hz采样
T = 0:1/Fs:3-1/Fs;
sound_c = Amp(1)*sin(2*pi*f(1)*T + ph(1));%声音信号
stem(T(1:N),sound_c(1:N))%前N个采样
title('前300个采样信号—时间图')
text(0,-5,'采样频率1k 相位为0')
xlabel('t(s)')
ylabel('A');

pause(1)

figure('name','第二题')
Fs = 8000;%采样频率
T = 0:1/Fs:3-1/Fs;%时间
N = 300;
for K = 1:3
sound_c = Amp(K)*sin(2*pi*f(K)*T + ph(K));%声音信号
subplot(4,1,K)%画图
if (K == 1) 
    stem(T(1:N),sound_c(1:N),':diamondr')%前N个采样
end
if(K == 2)
    stem(T(1:N),sound_c(1:N),'LineStyle','-.',...
        'MarkerFaceColor','red')%前N个采样
end
if(K == 3)
    h = stem(T(1:N),sound_c(1:N));%前N个采样
    h.Color = 'm';
    h.MarkerFaceColor = 'y';
end
pause(0.3);%延时0,3秒
sound(sound_c,Fs);%以采样频率播放声音
end

sumSound = zeros(1,length(T));%和弦声音
for K = 1:3
    sound_c = Amp(K)*sin(2*pi*f(K)*T + ph(K));%声音信号
    sumSound = sumSound + sound_c;
end
subplot(4,1,4)
stem(T(1:N),sumSound(1:N))
sound(sumSound,Fs);
